<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller
{
    //验证通过跳转到后台主页
    public function index()
    {
        // var_dump($_SESSION);exit;
        //接收用户名和密码
        $admin_name = I('post.name');
        $admin_pwd = I('post.pwd');
        $vcode = I('post.vcode');

        function check_verify($code, $id = ''){
            $verify = new \Think\Verify();
            return $verify->check($code, $id);
        }

        //验证验证码
        if (!check_verify($vcode)) {
            $this->error('验证码错误');
            exit;
        }

        //以用户名查数据
        $where['name'] = $admin_name;
        $admin_user = M('admin_user');
        $data = $admin_user->field('id, name, pwd')->where($where)->find();

        //验证用户名
        if (!$data) {
            $this->error('用户名不存在');
            exit;
        }

        //验证密码
        if ($data['pwd'] != md5($admin_pwd)) {
            $this->error('密码错误');
            exit;
        }

        //把用户信息添加到session
		$_SESSION['admin_user'] = $data;
        // v($_SESSION);exit;
        $this->success('登录成功', U('Index/index'));

    }

    //后台登录
    public function login()
    {
        $this->assign('title', '虎嗅后台登录');
        $this->display('Login/index');
    }

    //验证码
    public function vcode()
    {
        $config = array(
            'fontSize' => 20, // 验证码字体大小
            'length' => 4, // 验证码位数
            'useNoise' => false, // 关闭验证码杂点
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    //登出
    public function logout()
    {
        unset($_SESSION['admin_user']);
        $this->redirect('Login/login', '登出成功');
    }
}
